package com.yuma.rabbitmq.work.main;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.yuma.rabbitmq.work.util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Productor {
    private static final String QUEUE_NAMEA = "queue_usa";
    private static final String QUEUE_NAMEB = "queue_cn";
    private static final String QUEUE_NAMEC = "queue_news";
    private static final String QUEUE_NAMED = "queue_sports";
    private static final String QUEUE_NAMEE = "queue_all";
    private static final String EXCHANGE_NAME = "test_topic";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        // 定义交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true);
        channel.queueDeclare(QUEUE_NAMEA, false, false, false, null);
        channel.queueDeclare(QUEUE_NAMEB, false, false, false, null);
        channel.queueDeclare(QUEUE_NAMEC, false, false, false, null);
        channel.queueDeclare(QUEUE_NAMED, false, false, false, null);
        channel.queueDeclare(QUEUE_NAMEE, false, false, false, null);

        // 绑定
        channel.queueBind(QUEUE_NAMEA, EXCHANGE_NAME, "#.usa");//
        channel.queueBind(QUEUE_NAMEB, EXCHANGE_NAME, "#.cn");  //
        channel.queueBind(QUEUE_NAMEC, EXCHANGE_NAME, "news.*"); //
        channel.queueBind(QUEUE_NAMED, EXCHANGE_NAME, "sports.*"); //
        channel.queueBind(QUEUE_NAMEE, EXCHANGE_NAME, "*.*");   // 匹配所有的queue

        // 发送的时候：交换机、路由键、消息  和队列没关系
        channel.basicPublish(EXCHANGE_NAME, "news.cn", null, "这是一条中国的新闻".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "sports.usa", null, "橄榄球是美国的一项运动".getBytes());

        channel.close();
        connection.close();
    }
}
